package com.syni.mdd.sjb.common.datatreaure.controller.protect.dataTreasure.business;

import com.alibaba.fastjson.JSON;
import com.github.houbb.paradise.common.util.CollectionUtil;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsApplication;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.GetBusinessRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.ManagementHomePageRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.GetMyStoreRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.BaseRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataApplicationVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataBankBusinessVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.MinAppOrdersRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.UnifiedCodeQueryVo;
import com.syni.mdd.sjb.common.datatreaure.service.*;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankBusinessService;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/databank/business")
@Log4j2
public class BankBusinessController {

    @Autowired
    DataBankUserService dataBankUserService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    DataBankBusinessService dataBankBusinessService;

    @Autowired
    DankApplicationService dankApplicationService;

    @Autowired
    DankMerchantsApplicationService dankMerchantsApplicationService;

    @Autowired
    DankBusinessVerRecordService dankBusinessVerRecordService;

    @Autowired
    DxshAppIndustryChooseService dxshAppIndustryChooseService;

    @Autowired
    DankMerchantsDefBusinessService dankMerchantsDefBusinessService;

    @Value("${dxshappIndustry.industry.type.json}")
    private String industryJson;

    public static Map<String,String> industryTypeMap = new HashMap<>();

    public static Map<String,List<String>> typeIndustryMap = new HashMap<>();

    static {
        //初始化映射关系
        /*industryTypeMap.put("蔬果","新零售");
        industryTypeMap.put("购物","新零售");
        industryTypeMap.put("美食","餐饮美食");
        industryTypeMap.put("旅游","通用");
        industryTypeMap.put("医疗","通用");
        industryTypeMap.put("玩乐","通用");
        industryTypeMap.put("丽人","通用");
        industryTypeMap.put("培训","通用");
        industryTypeMap.put("儿童","通用");
        industryTypeMap.put("影视","通用");
        industryTypeMap.put("团队","通用");
        industryTypeMap.put("美容","通用");
        industryTypeMap.put("桌游","通用");
        industryTypeMap.put("婚礼","通用");
        industryTypeMap.put("养生","通用");

        //初始化映射关系
        typeIndustryMap.put("新零售", Arrays.asList(new String[]{"蔬果","购物"}));
        typeIndustryMap.put("通用", Arrays.asList(new String[]{"旅游","医疗","玩乐","丽人","培训","儿童","影视",
                "团队","美容","桌游","婚礼","养生"}));
        typeIndustryMap.put("餐饮美食", Arrays.asList(new String[]{"美食"}));*/
    }

    @PostConstruct
    public void init(){
        industryTypeMap = JSON.parseObject(industryJson,Map.class);
        for(Map.Entry<String,String> entry : industryTypeMap.entrySet()){
            if(!typeIndustryMap.containsKey(entry.getValue())){
                typeIndustryMap.put(entry.getValue(),new ArrayList<>());
            }
            typeIndustryMap.get(entry.getValue()).add(entry.getKey());
        }
        log.info("industryTypeMap:{}",JSON.toJSONString(industryTypeMap));
        log.info("typeIndustryMap:{}",JSON.toJSONString(typeIndustryMap));


    }



    @PostMapping("/getTargetBusiness")
    @ApiOperation(value = "觅东东数据宝获取我的店铺")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId",value = "店铺id")})
    @CheckUserIdentity
    public Map<String,Object> getTargetBusiness(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        return dxshAppUserChannelService.getTargetBusiness(dataBankBusinessVo);
    }

    @PostMapping("/searchAlreadyBusiness")
    @ApiOperation(value = "获取我认领过的店铺")
    @CheckUserIdentity
    public Map<String,Object> searchAlreadyBusiness(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        return dataBankBusinessService.searchAlreadyBusiness(dataBankBusinessVo);
    }


    @PostMapping("/chooseDefaultBusiness")
    @ApiOperation(value = "选择一个默认的店铺")
    @CheckUserIdentity
    public Map<String,Object> chooseDefaultBusiness(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        if(!dataBankBusinessService.chooseDefaultBusiness(dataBankBusinessVo.getUserId(),dataBankBusinessVo.getBusinessId())){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.HAND_FAIL);
        }
        //切换成功后获取店铺信息
        Integer businessId = dankMerchantsDefBusinessService.getChooseBusinessId(dataBankBusinessVo.getUserId());
        dataBankBusinessVo.setBusinessId(businessId);
        Map<String,Object> resultMap = dataBankBusinessService.getBusiness(dataBankBusinessVo);
        if(ResultMapHelper.isSuccess(resultMap)){
            Map<String,Object> data = (Map<String, Object>) resultMap.get("data");
            GetBusinessRpVo getBusinessRpVo = (GetBusinessRpVo) data.get("business");
            Map<String,Object> returnMap = new HashMap<>();
            returnMap.put("business",getBusinessRpVo);
            return ResultMapHelper.success(returnMap,ResultMsg.HAND_SUCCESS);
        }
        return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.HAND_FAIL);
//        BankBusinessBo bankBusinessBo = dataBankBusinessService.getChooseBusinessData(dataBankBusinessVo.getBusinessId());
//        if(bankBusinessBo == null){
//            return ResultMapHelper.result(ResultCode.CODE_ERROR_100,ResultMsg.HAND_FAIL);
//        }
//        ChooseDefaultBusinessRpVo chooseDefaultBusinessRpVo = BeanUtils.copyProperties(bankBusinessBo,ChooseDefaultBusinessRpVo.class);
//        if(StringUtils.isNotBlank(chooseDefaultBusinessRpVo.getTypeName())){
//            if(industryTypeMap.containsKey(chooseDefaultBusinessRpVo.getTypeName())){
//                chooseDefaultBusinessRpVo.setIndustryName(industryTypeMap.get(chooseDefaultBusinessRpVo.getTypeName()));
//            }
//        }
//        Map<String,Object> resultMap = new HashMap<>();
//        resultMap.put("business",chooseDefaultBusinessRpVo);
//        return ResultMapHelper.success(resultMap,ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/getBusiness")
    @ApiOperation(value = "获取店铺信息")
    @CheckUserIdentity
    public Map<String,Object> getBusiness(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        return dataBankBusinessService.getBusiness(dataBankBusinessVo);
    }

    @PostMapping("/managementHomePage")
    @ApiOperation(value = "经营首页（上半部分）")
    @CheckUserIdentity
    public Map<String,Object> managementHomePage(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        ManagementHomePageRpVo managementHomePageRpVo = new ManagementHomePageRpVo();
        //上面的几种渠道
        Map<String,Object> retMap = dxshAppUserChannelService.getMyStore(dataBankBusinessVo.getUserId(),dataBankBusinessVo.getBusinessId());
        if(ResultMapHelper.isSuccess(retMap)){
            GetMyStoreRpVo getMyStoreRpVo = (GetMyStoreRpVo) retMap.get("data");
            if(CollectionUtils.isNotEmpty(getMyStoreRpVo.getStores())){
                GetMyStoreRpVo.StoresVo storesVo = getMyStoreRpVo.getStores().get(0);
                managementHomePageRpVo.setBusinessId(storesVo.getBusinessId());
                managementHomePageRpVo.setVendorName(storesVo.getVendorName());
                List<GetMyStoreRpVo.ChannelVo> channelVos = storesVo.getChannels();
                if(CollectionUtils.isNotEmpty(channelVos)){
                    for(GetMyStoreRpVo.ChannelVo channelVo : channelVos){
                        ManagementHomePageRpVo.ManagementHomePageChannels managementHomePageChannels = new ManagementHomePageRpVo.ManagementHomePageChannels();
                        BeanUtils.copyProperties(channelVo,managementHomePageChannels);
                        managementHomePageRpVo.getChannels().add(managementHomePageChannels);
                    }
                }
            }
        }
        return ResultMapHelper.success(managementHomePageRpVo,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/managementHomePage/v2")
    @ApiOperation(value = "经营首页（上半部分）")
    @CheckUserIdentity
    public Map<String,Object> managementHomePage_v2(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){
        return dataBankBusinessService.managementHomePage_v2(dataBankBusinessVo);
    }

    @PostMapping("/managementExclusiveData")
    @ApiOperation(value = "下面可切换列表")
    @CheckUserIdentity
    public Map<String,Object> managementExclusiveData(@RequestBody @Validated DataBankBusinessVo dataBankBusinessVo){

        //根据channelId调用远程的接口
        return ResultMapHelper.success(null,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/getApplicationList")
    @ApiOperation(value = "获取应用中心应用列表")
    public Map<String,Object> getApplicationList(){
        return ResultMapHelper.success(dankApplicationService.getApplicationList(),ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/saveBusinessApp")
    @ApiOperation(value = "保存商户应用列表")
    @CheckUserIdentity
    public Map<String,Object> saveBusinessApp(@RequestBody @Validated DataApplicationVo dataApplicationVo){
        DankMerchantsApplication dankMerchantsApplication = null;
        List<DankMerchantsApplication> homePageService = dankMerchantsApplicationService.getHomePageService(dataApplicationVo.getUserId());
        if(CollectionUtil.isEmpty(homePageService)){
            dankMerchantsApplication = new DankMerchantsApplication();
            dankMerchantsApplication.setUserId(dataApplicationVo.getUserId());
        }else{
            dankMerchantsApplication = homePageService.get(0);
        }
        dankMerchantsApplication.setAppId(dataApplicationVo.getAppIds());
        dankMerchantsApplicationService.saveOrUpdate(dankMerchantsApplication);
        return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/merchantApp")
    @ApiOperation(value = "商家应用列表")
    @CheckUserIdentity
    public Map<String,Object> merchantApp(@RequestBody BaseRqVo baseRqVo){
        return ResultMapHelper.success(dankMerchantsApplicationService.getMerchantApp(baseRqVo.getUserId()),ResultMsg.HAND_SUCCESS);
    }

    @PostMapping("/getMddGroupBuyCouponDetail")
    @ApiOperation(value = "查询觅东东团购券内容详情")
    @CheckUserIdentity
    public Map<String,Object> getMddGroupBuyCouponDetail(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dankBusinessVerRecordService.getMddGroupBuyCouponDetail(unifiedCodeQueryVo);
    }


    @PostMapping("/unifiedCodeQuery")
    @ApiOperation(value = "统一类型券查询接口")
    @CheckUserIdentity
    public Map<String,Object> unifiedCodeQuery(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dankBusinessVerRecordService.unifiedCodeQuery(unifiedCodeQueryVo);
    }

    @PostMapping("/unifiedVoucherCode")
    @ApiOperation(value = "统一类型券核销接口")
    @CheckUserIdentity
    public Map<String,Object> unifiedVoucherCode(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dankBusinessVerRecordService.unifiedVoucherCode(unifiedCodeQueryVo);
    }


    @PostMapping("/marketMddOrders")
    @ApiOperation(value = "经营觅东东订单")
    @CheckUserIdentity
    public Map<String,Object> marketMddOrders(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dataBankBusinessService.marketMddOrders(unifiedCodeQueryVo);
    }

    @PostMapping("/minAppOrders")
    @ApiOperation(value = "经营小程序订单")
    @CheckUserIdentity
    public Map<String,Object> minAppOrders(@RequestBody MinAppOrdersRqVo minAppOrdersRqVo){
        return dataBankBusinessService.minAppOrders(minAppOrdersRqVo);
    }

    @PostMapping("/dataBankAnalysisRevenue")
    @ApiOperation(value = "营收")
    @CheckUserIdentity
    public Map<String,Object> dataBankAnalysisRevenue(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dataBankBusinessService.dataBankAnalysisRevenue(unifiedCodeQueryVo);
    }

    @PostMapping("/dataBankAnalysisOrder")
    @ApiOperation(value = "订单")
    @CheckUserIdentity
    public Map<String,Object> dataBankAnalysisOrder(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dataBankBusinessService.dataBankAnalysisOrder(unifiedCodeQueryVo);
    }

    @PostMapping("/dataBankAnalysisFlow")
    @ApiOperation(value = "流量")
    @CheckUserIdentity
    public Map<String,Object> dataBankAnalysisFlow(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dataBankBusinessService.dataBankAnalysisFlow(unifiedCodeQueryVo);
    }

    @PostMapping("/dataBankAnalysisProduct")
    @ApiOperation(value = "商品")
    @CheckUserIdentity
    public Map<String,Object> dataBankAnalysisProduct(@RequestBody UnifiedCodeQueryVo unifiedCodeQueryVo){
        return dataBankBusinessService.dataBankAnalysisProduct(unifiedCodeQueryVo);
    }
}
